Micron Document
🎖️GitЯра🎖️


Displaying Raw • Download

core/data/src/commonMain/kotlin/org/meshtastic/core/data/repository/DeviceLinkMatcher.kt 7631db63d0bdff28078438b095c0259306cb6617 (7631db63) Text, 4.91 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.data.repository

Tff7b72import T7ee787org.meshtastic.core.model.DeviceLink

T8b949e/**
* Pure matching logic for associating msh.to [DeviceLink]s with a device's `platformioTarget`. Ported from the
* Meshtastic-Apple `DeviceLinksSection` (multi-tier matching: exact vendor, product variant, marketplace), so the two
* platforms surface the same links.
*/
Tff7b72object T56d364DeviceLinkMatcher Tb4b4b4{

T8b949e/**
* Links relevant to [target], region-filtered and sorted with vendor/variant links first.
*
* @param links all imported links.
* @param marketplaceKeys known marketplace identifiers (from `marketplaces.json`).
* @param deviceTargets all known device `platformioTarget`s — used to exclude other devices' links.
* @param target the viewed device's `platformioTarget`.
* @param region the user's ISO 3166-1 alpha-2 region for marketplace filtering.
*/
Tff7b72fun Td2a8ffmatchTb4b4b4(
Te6edf3linksTb4b4b4: Te6edf3ListTff7b72<Te6edf3DeviceLinkTff7b72>Tb4b4b4,
Te6edf3marketplaceKeysTb4b4b4: Te6edf3SetTff7b72<Tffa657StringTff7b72>Tb4b4b4,
Te6edf3deviceTargetsTb4b4b4: Te6edf3SetTff7b72<Tffa657StringTff7b72>Tb4b4b4,
Te6edf3targetTb4b4b4: Tffa657StringTb4b4b4,
Te6edf3regionTb4b4b4: Tffa657StringTb4b4b4,
Tb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Te6edf3DeviceLinkTff7b72> Tb4b4b4{
Tff7b72val Te6edf3variants Tff7b72= Te6edf3buildTargetVariantsTb4b4b4(Te6edf3targetTb4b4b4)
Tff7b72return Te6edf3links
Tb4b4b4.Te6edf3filter Tb4b4b4{ Te6edf3link Tff7b72-Tff7b72> Te6edf3matchesTb4b4b4(Te6edf3linkTb4b4b4, Te6edf3marketplaceKeysTb4b4b4, Te6edf3deviceTargetsTb4b4b4, Te6edf3targetTb4b4b4, Te6edf3variantsTb4b4b4, Te6edf3regionTb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3sortedByDescending Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3isVendor Tff7b72|Tff7b72| Tff7b72!Te6edf3isMarketplaceLinkTb4b4b4(Tffa657itTb4b4b4.Te6edf3shortCodeTb4b4b4, Te6edf3marketplaceKeysTb4b4b4) Tb4b4b4}
Tb4b4b4}

Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffReturnCountTa5d6ff"Tb4b4b4)
Tff7b72private Tff7b72fun Td2a8ffmatchesTb4b4b4(
Te6edf3linkTb4b4b4: Te6edf3DeviceLinkTb4b4b4,
Te6edf3marketplaceKeysTb4b4b4: Te6edf3SetTff7b72<Tffa657StringTff7b72>Tb4b4b4,
Te6edf3deviceTargetsTb4b4b4: Te6edf3SetTff7b72<Tffa657StringTff7b72>Tb4b4b4,
Te6edf3targetTb4b4b4: Tffa657StringTb4b4b4,
Te6edf3variantsTb4b4b4: Te6edf3ListTff7b72<Tffa657StringTff7b72>Tb4b4b4,
Te6edf3regionTb4b4b4: Tffa657StringTb4b4b4,
Tb4b4b4)Tb4b4b4: Tffa657Boolean Tb4b4b4{
Tff7b72val Te6edf3code Tff7b72= Te6edf3linkTb4b4b4.Te6edf3shortCode

T8b949e// Exact vendor match always wins.
Tff7b72if Tb4b4b4(Te6edf3code Tff7b72=Tff7b72= Te6edf3targetTb4b4b4) Tff7b72return Tff7b72true

T8b949e// A vendor link for a different device is never shown here.
Tff7b72if Tb4b4b4(Te6edf3linkTb4b4b4.Te6edf3isVendor Tff7b72&Tff7b72& Te6edf3code Tff7b72!Tff7b72= Te6edf3targetTb4b4b4) Tff7b72return Tff7b72false

T8b949e// Variant/marketplace-suffix: "<target>-..." or "<target>_...".
Tff7b72val Te6edf3matchesPrefix Tff7b72= Te6edf3variantsTb4b4b4.Te6edf3any Tb4b4b4{ Te6edf3codeTb4b4b4.Te6edf3startsWithTb4b4b4(Ta5d6ff"Tffd700${Tffa657itTffd700}Ta5d6ff_Ta5d6ff"Tb4b4b4) Tff7b72|Tff7b72| Te6edf3codeTb4b4b4.Te6edf3startsWithTb4b4b4(Ta5d6ff"Tffd700$Te6edf3itTa5d6ff-Ta5d6ff"Tb4b4b4) Tb4b4b4}

T8b949e// Known marketplace prefix: "<marketplace>-<target>" or "<marketplace>_<target>".
Tff7b72val Te6edf3matchesMarketplacePrefix Tff7b72=
Te6edf3variantsTb4b4b4.Te6edf3any Tb4b4b4{ Te6edf3variant Tff7b72-Tff7b72> Te6edf3marketplaceKeysTb4b4b4.Te6edf3any Tb4b4b4{ Te6edf3mp Tff7b72-Tff7b72> Te6edf3code Tff7b72=Tff7b72= Ta5d6ff"Tffd700$Te6edf3mpTa5d6ff-Tffd700$Te6edf3variantTa5d6ff" Tff7b72|Tff7b72| Te6edf3code Tff7b72=Tff7b72= Ta5d6ff"Tffd700${Te6edf3mpTffd700}Ta5d6ff_Tffd700$Te6edf3variantTa5d6ff" Tb4b4b4} Tb4b4b4}

Tff7b72if Tb4b4b4(Tff7b72!Te6edf3matchesPrefix Tff7b72&Tff7b72& Tff7b72!Te6edf3matchesMarketplacePrefixTb4b4b4) Tff7b72return Tff7b72false

T8b949e// A prefix hit that is itself a different device's target belongs to that device, not this one.
Tff7b72if Tb4b4b4(Te6edf3matchesPrefix Tff7b72&Tff7b72& Te6edf3code Tff7b72in Te6edf3deviceTargets Tff7b72&Tff7b72& Te6edf3code Tff7b72!Tff7b72= Te6edf3targetTb4b4b4) Tff7b72return Tff7b72false

T8b949e// Region filter: null regions = vendor/variant (always), empty = worldwide, else must include the region.
Tff7b72val Te6edf3regions Tff7b72= Te6edf3linkTb4b4b4.Te6edf3regions Tff7b72?: Tff7b72return Tff7b72true
Tff7b72if Tb4b4b4(Te6edf3regionsTb4b4b4.Te6edf3isEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Tff7b72return Tff7b72true
Tff7b72return Te6edf3region Tff7b72in Te6edf3regions
Tb4b4b4}

T8b949e/** True when [code] carries a known marketplace prefix or suffix. */
Tff7b72fun Td2a8ffisMarketplaceLinkTb4b4b4(Te6edf3codeTb4b4b4: Tffa657StringTb4b4b4, Te6edf3marketplaceKeysTb4b4b4: Te6edf3SetTff7b72<Tffa657StringTff7b72>Tb4b4b4)Tb4b4b4: Tffa657Boolean Tff7b72=
Te6edf3marketplaceKeyForTb4b4b4(Te6edf3codeTb4b4b4, Te6edf3marketplaceKeysTb4b4b4) Tff7b72!Tff7b72= Tff7b72null

T8b949e/**
* The marketplace identifier [code] belongs to (as a delimiter-bounded prefix `mp-`/`mp_` or suffix `-mp`/`_mp`),
* or `null` if none. This is the single source of truth for "is this a marketplace link" — used for import-time
* region tagging, sort ordering, and UI prominence — so the classifications never disagree. Delimiter bounds avoid
* mis-tagging codes that merely begin with a marketplace name (e.g. `muziworks` is NOT `muzi`).
*/
Tff7b72fun Td2a8ffmarketplaceKeyForTb4b4b4(Te6edf3codeTb4b4b4: Tffa657StringTb4b4b4, Te6edf3marketplaceKeysTb4b4b4: Te6edf3SetTff7b72<Tffa657StringTff7b72>Tb4b4b4)Tb4b4b4: Tffa657String? Tff7b72= Te6edf3marketplaceKeysTb4b4b4.Te6edf3firstOrNull Tb4b4b4{ Te6edf3mp Tff7b72-Tff7b72>
Te6edf3codeTb4b4b4.Te6edf3startsWithTb4b4b4(Ta5d6ff"Tffd700$Te6edf3mpTa5d6ff-Ta5d6ff"Tb4b4b4) Tff7b72|Tff7b72| Te6edf3codeTb4b4b4.Te6edf3startsWithTb4b4b4(Ta5d6ff"Tffd700${Te6edf3mpTffd700}Ta5d6ff_Ta5d6ff"Tb4b4b4) Tff7b72|Tff7b72| Te6edf3codeTb4b4b4.Te6edf3endsWithTb4b4b4(Ta5d6ff"Ta5d6ff-Tffd700$Te6edf3mpTa5d6ff"Tb4b4b4) Tff7b72|Tff7b72| Te6edf3codeTb4b4b4.Te6edf3endsWithTb4b4b4(Ta5d6ff"Ta5d6ff_Tffd700$Te6edf3mpTa5d6ff"Tb4b4b4)
Tb4b4b4}

T8b949e/**
* Alternate target strings for matching. Strips a leading `rak` (e.g. `rak4631` → `4631`) to absorb msh.to naming
* inconsistencies like `rokland-4631`.
*/
Tff7b72fun Td2a8ffbuildTargetVariantsTb4b4b4(Te6edf3targetTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Tffa657StringTff7b72> Tb4b4b4{
Tff7b72val Te6edf3variants Tff7b72= Te6edf3mutableListOfTb4b4b4(Te6edf3targetTb4b4b4)
Tff7b72if Tb4b4b4(Te6edf3targetTb4b4b4.Te6edf3startsWithTb4b4b4(Ta5d6ff"Ta5d6ffrakTa5d6ff"Tb4b4b4)Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3stripped Tff7b72= Te6edf3targetTb4b4b4.Te6edf3removePrefixTb4b4b4(Ta5d6ff"Ta5d6ffrakTa5d6ff"Tb4b4b4)
Tff7b72if Tb4b4b4(Te6edf3strippedTb4b4b4.Te6edf3isNotEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Te6edf3variantsTb4b4b4.Te6edf3addTb4b4b4(Te6edf3strippedTb4b4b4)
Tb4b4b4}
Tff7b72return Te6edf3variants
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.08s